<?php
require_once(dirname(__FILE__)."/../common/constants.php");
require_once(DB_SESSION_DIR_PATH."/db_operator.php");
require_once(DB_SESSION_DIR_PATH."/db_obj_defs.php");
require_once(TRANSACTIONS_DIR_PATH."/trans_common.php");
require_once(TRANSACTIONS_DIR_PATH."/pagicalendarmgr.php");
require_once(TRANSACTIONS_DIR_PATH."/calendarmanager.php");
/*
 * Pattern of the HTTP request for posting calendar item
 * name:   user name
 * pwd:    user password
 * by:     name is sent as name (TRUE, if it's user name.)
 * t:      action type you request to perform (1: Get calendar; 2: Post calendar)
 * sta: start time
 * end: end time
 * ala: alarm time
 * sum: summary
 * not: note
 * loc: location
 */
class HttpPostCalendarListener implements IHttpListener {
	public function onRequestReceived($params) {
		$start_time	= null;
		$end_time	= null;
		$alarm		= null;
		$summary	= null;
		$note		= null;
		$location	= null;
		
		if (isset($params['sta']))
			$start_time = $params['sta'];
		if (isset($params['end']))
			$end_time = $params['end'];
		if (isset($params['ala']))
			$alarm = $params['ala'];
		if (isset($params['sum']))
			$summary = $params['sum'];
		if (isset($params['not']))
			$note = $params['not'];
		if (isset($params['loc']))
			$location = $params['loc'];
		
		$c_mgr = new CalendarManager(NULL,
									 $start_time,
									 $end_time,
									 $alarm,
									 $summary,
									 $note,
									 $location);
		$stat = $c_mgr->register();
		
		if (!DbError::is_error($stat))
			return new HttpError(HttpError::HTTP_OK, 'Saving done.');
		else
			return new HttpError(HttpError::HTTP_CALENDAR_SAVE_FAILED, 'Failed to save calendar. Try again.');
	}
}

/*
 * Pattern of the HTTP request for querying calendar item
 * name:user name
 * pwd: user password
 * by:  name is sent as name (TRUE, if it's user name.)
 * t:   action type you request to perform (1: Get calendar; 2: Post calendar)
 * p:	page number if pagination is required
 * cnt:	count of items per page.
 * cop: total count of pages.
 */
class HttpGetCalendarListener implements IHttpListener, IHttpOutputable {
	private $calList;
	
	public function __construct() {
		$this->calList = array();
	}
	
	public function onRequestReceived($params) {
		$uid		= null;
		$start_time	= null;
		$end_time	= null;
		$alarm		= null;
		$summary	= null;
		$note		= null;
		$location	= null;
		
		if (isset($params['uid']))
			$uid = $params['uid'];
		
		if (isset($params['p']))
			$page_num = $params['p'];
		else
			$page_num = 1;
		
		if (isset($params['cnt']))
			$cnt_per_page = $params['cnt'];
		else
			$cnt_per_page = 5;
			
		if (isset($params['cop']))
			$cntOfPages = $params['cop'];
		else
			$cntOfPages = -1;
		
		$op = new PagiCalendarManager();
		$op->config(CONFIGURATION_DIR_PATH."/calendar_table.xml");
		if ($cntOfPages != -1)
			$op->setCountOfPage($cntOfPages);
		
		$cond_list = new SqlCondList();
		$cond_list->add_condition(new SqlCondition("uid", SqlCondition::EQUAL, $uid));
		$cond_list->add_constraint(new SqlConstraint(SqlConstraint::ORDERBY, "ts_start"));
		$cond_list->add_sort(new SqlSort(SqlSort::ASC));
	
		$op->setCondition($cond_list);
		$op->go2page($page_num, $cnt_per_page, true);
	
		$this->calList = $op->getResultSet();
		
		return new HttpError(HttpError::HTTP_OK, 'Query done.');
	}
	
	public function output() {
		$output = '';
		if ($this->calList != null) {
			foreach ($this->calList as $calItem) {
				$output .= $calItem->toString()."\n";
			}
		}
		
		return $output;
	}
}

?>